
package JET.Frames.recorderframe;

import JET.Centers.ControlCenter;
import JET.recorder.Recorder;
import JET.recorder.BodyHelper;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.swing.JPanel;

/**
 *
 * @author 057Ry
 */
public class RecorderChartPanel extends JPanel
{
    static final ControlCenter CC = ControlCenter.getInstance();
    Recorder rec;
    Graphics2D g2d;
    double vX=0;           // view X
    double vY=0;           // view Y
    double scaleX = 0.1;
    double scaleY = 0.1;

    public RecorderChartPanel(Recorder r) {
        rec = r;
        
        Dimension dim = new Dimension(600,300);
        this.setMaximumSize(dim);
        this.setMinimumSize(dim);
        this.setPreferredSize(dim);

        this.addMouseMotionListener( new RecCgartPanelMouseMotionListener(this) );
        this.addMouseWheelListener( new RecordChartPanelMoueWheelListener(this) );
        this.setBackground(Color.WHITE);
    }

    public double graphToScrX(double X)
    {
        return vX + X*scaleX;
    }

    public double graphToScrY(double Y)
    {
        return vY + Y*-1*scaleY + this.getHeight()/2;
    }

    public double g2sX(double X)
    {
        return X*scaleX;
    }

    public double g2sY(double Y)
    {
        return Y*-1*scaleY;
    }

    public void setScaleX(double s) {
        scaleX = s;
    }
    public void setScaleY(double s) {
        scaleY = s;
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g2d = (Graphics2D)g;
        System.out.println("vX="+vX+" vY="+vY);
        
        g2d.setColor(Color.LIGHT_GRAY);
        drawGrid(50, 50, 0,0);

        g2d.setColor(Color.BLACK);
        drawGrid(10000000, 10000000, 0,1);

        Enumeration elements = rec.getRecTable().elements();
        while( elements.hasMoreElements() )
        {
            ArrayList<BodyHelper> list = (ArrayList<BodyHelper>) elements.nextElement();
            drawKinecticEnergy(list);
            drawVelocity(list);
            drawPotentialEnergy(list);
        }
    }

    public double scrToGraphX(double x) {
        return (x-vX)/scaleX;
    }

    public double scrToGraphY(double y) {
        return (y-vY-getHeight()/2)/-scaleY;
    }

    private void drawGrid(double dx, double dy, int start, int finish)
    {
        // Vertical lines
        double x = 0.;
        double dx_scaled = dx*scaleX;
        x += vX%dx_scaled;

        while( x<=getWidth() )
        {
            for(int i=start; i<=finish; i++)
                g2d.draw( new Line2D.Double(x+i,0, x+i,getHeight()) );
            
            x += dx_scaled;
        }

        // Horizontal lines
        double y = 0.;
        double dy_scaled = dy*scaleY;
        y += (vY+getHeight()/2)%dy_scaled;
        while( y<=getHeight() )
        {
            for(int i=start; i<=finish; i++)
                g2d.draw( new Line2D.Double(0,y+i, getWidth(),y+i) );
            
            y += dy_scaled;
        }
    }

    private void drawKinecticEnergy(ArrayList<BodyHelper> list)
    {
        double lastX = 0.;
        double lastY = 0.;
        double x,y;

        g2d.setColor(Color.BLUE);
        for(BodyHelper sbr : list)
        {
//            x = sbr.getTime();// * scaleX;
//            y = sbr.getKineticEnergy()/100;
//            g2d.draw( new Line2D.Double(graphToScrX(lastX),graphToScrY(lastY), graphToScrX(x),graphToScrY(y)) );
//            lastX = x;
//            lastY = y;
        }
    }
    private void drawPotentialEnergy(ArrayList<BodyHelper> list)
    {
        double lastX = 0.;
        double lastY = 0.;
        double x,y;

        g2d.setColor(Color.RED);
        for(BodyHelper sbr : list)
        {
//            x = sbr.getTime();// * scaleX;
//            throw new RuntimeException("gravity now is property of engine not CC !");
//            y = sbr.getPotentialEnergy(CC.gravityValue)/100;
//            g2d.draw( new Line2D.Double(graphToScrX(lastX),graphToScrY(lastY), graphToScrX(x),graphToScrY(y)) );
//            lastX = x;
//            lastY = y;
        }
    }
     private void drawVelocity(ArrayList<BodyHelper> list)
    {
        double lastX = 0.;
        double lastY = 0.;
        double x,y;

        g2d.setColor(Color.GREEN);
        for(BodyHelper sbr : list)
        {
//            x = sbr.getTime();// * scaleX;
//            y = sbr.getVelocity().getMagnitude();
//            g2d.draw( new Line2D.Double(graphToScrX(lastX),graphToScrY(lastY), graphToScrX(x),graphToScrY(y)) );
//            lastX = x;
//            lastY = y;
        }
    }

}
